﻿@page "/useractions"

@using Gurux.DLMS.AMI.Shared.DIs
@using Gurux.DLMS.AMI.Shared.Enums

@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs
@using Gurux.DLMS.AMI.Shared.DTOs.Authentication
@using Gurux.DLMS.AMI.Shared.Rest
@using Microsoft.AspNetCore.SignalR.Client
@using Gurux.DLMS.AMI.Shared
@using Gurux.DLMS.AMI.Module
@using Gurux.DLMS.AMI.Client.Helpers

@attribute [Authorize(Roles = "Admin, UserManager, User")]
@inject NavigationManager NavigationManager
@inject HttpClient Http
@inject IGXNotifier Notifier
@inject ConfirmBase Confirm
@implements IDisposable
@implements IAmiComponent
@if (Title)
{
    <PageTitle>@Properties.Resources.Actions</PageTitle>
}

<AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.UserManager, GXRoles.User)>
    <MenuControl RightCorner="true">
        <ChildContent>
            <MenuItem Text="@Properties.Resources.Clear" Icon="oi oi-trash" OnClick="@OnClear" />
        </ChildContent>
    </MenuControl>
</AuthorizeView>

<GXTable @ref="table"
         Context="action"
         ItemsProvider="@GetItems"
         SelectionMode="SelectionMode.None"
         ShowRemoved="false"
         Filter="@Filter"
         ShowAllUsers="@Header"
         Columns="@Columns"
         CanEdit="@CanEdit"
         OnSearch="@Updated">
    <FilterContent>
        <th>
            <input class="form-control" placeholder="按创建时间..."
                   type="datetime-local"
                   @onchange="@((ChangeEventArgs __e) => filter.CreationTime = Convert.ToDateTime(__e.Value))" />
        </th>
        <th>
            <input class="form-control" type="text" placeholder="按用户名..."
                   @onchange="@((ChangeEventArgs __e) => filter.User.UserName = Convert.ToString(__e.Value))" />
        </th>
        <th>
        </th>
    </FilterContent>
    <MenuContent>
        <ContextMenuItem Text="@Properties.Resources.Show" Icon="oi oi-eye" OnClick="@OnShow"></ContextMenuItem>
    </MenuContent>
    <HeaderContent>
        <Th Id="CreationTime" SortMode="SortMode.Descending">@Properties.Resources.CreationTime</Th>
            <th>@Properties.Resources.User</th>
        <Th Id="Target">@Properties.Resources.Target</Th>
            <Th Id="Action">@Properties.Resources.Action</Th>
            <Th Id="Status">@Properties.Resources.Status</Th>
        </HeaderContent>
        <ItemContent>
            <td>@action.CreationTime</td>
        <td>@action.User?.UserName</td>
        <td>@action.TargetType</td>
        <td>@action.Action</td>
        <td>@action.Status</td>
    </ItemContent>
</GXTable>
<br />
<Confirm @ref="ClearConfirmation"
         ConfirmationChanged="OnClearConfirmation"
         ConfirmationTitle=@Properties.Resources.ConfirmDataClear
         AllowDelete="false"
         OkTitle="@Properties.Resources.Clear"
         ConfirmationMessage="@Properties.Resources.AreYouSureYouWantToClearLog">
</Confirm>

@code {
    /// <summary>
    /// Selected user action ID.
    /// </summary>
    [Parameter]
    public Guid? Id { get; set; }


    /// <inheritdoc />
    public string Name
    {
        get
        {
            return Gurux.DLMS.AMI.Client.Properties.Resources.UserActions;
        }
    }

    /// <inheritdoc />
    public Type? ConfigurationUI
    {
        get
        {
            return null;
        }
    }

    /// <inheritdoc />
    public string? Icon
    {
        get
        {
            return "oi oi-info";
        }
    }

    /// <summary>
    /// Amount of the user actions shown on the view.
    /// </summary>
    [Parameter]
    public int Count { get; set; } = 0;

    /// <summary>
    /// Is filter shown.
    /// </summary>
    [Parameter]
    public bool Filter { get; set; } = true;

    /// <summary>
    /// Is header shown.
    /// </summary>
    [Parameter]
    public bool Header { get; set; } = true;

    /// <summary>
    /// Is edit allowed.
    /// </summary>
    [Parameter]
    public bool CanEdit { get; set; } = true;

    /// <summary>
    /// Available columns.
    /// </summary>
    [Parameter]
    public string[]? Columns { get; set; }

    /// <summary>
    /// Is title shown.
    /// </summary>
    [Parameter]
    public bool Title { get; set; } = true;

    /// <summary>
    /// User action log filter.
    /// </summary>
    private GXUserAction filter = new GXUserAction()
        {
            User = new GXUser()
            {
                EmailConfirmed = null,
                PhoneNumberConfirmed = null,
                TwoFactorEnabled = null,
                LockoutEnabled = null
            }
        };

    /// <summary>
    /// User is verified before system errors are cleared.
    /// </summary>
    protected ConfirmBase? ClearConfirmation;

    public string? SelectedUser
    {
        get;
        private set;
    }

    /// <summary>
    /// Active item.
    /// </summary>
    public GXUserAction? Active
    {
        get
        {
            return table?.Active;
        }
    }

    /// <summary>
    /// Table reference.
    /// </summary>
    protected GXTable<GXUserAction>? table;

    /// <summary>
    /// Update table.
    /// </summary>
    protected async Task Updated()
    {
        try
        {
            if (table != null)
            {
                Notifier?.ClearStatus();
                await table.RefreshDataAsync(true);
            }
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }

    protected override async Task OnInitializedAsync()
    {
        //Wait until table is loaded. Don't remove this or table is null and last settings are not available.
        await Task.Delay(1);
        try
        {
            Notifier.ProgressStart();
            Notifier.ClearStatus();
            Notifier.On<IEnumerable<GXUserAction>>(this, nameof(IGXHubEvents.UserActionAdd), async (actions) =>
            {
                await Updated();
            });
            Notifier.On<IEnumerable<GXUserAction>>(this, nameof(IGXHubEvents.UserActionDelete), async (actions) =>
            {
                await Updated();
            });
            Notifier.On<IEnumerable<GXUser>?>(this, nameof(IGXHubEvents.UserActionClear), async (actions) =>
            {
                await Updated();
            });
            Notifier.Clear();
            Notifier.UpdateButtons();
            if (table != null && Id != null)
            {
                //Get last selected item.
                table.Active = new GXUserAction() { Id = Id.Value };
            }
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
    }

    private async ValueTask<ItemsProviderResult<GXUserAction>> GetItems(GXItemsProviderRequest request)
    {
        //Don't clear status or error is lost.
        Notifier?.ProgressStart();
        try
        {
            ListUserAction req = new ListUserAction()
                {
                    Index = request.StartIndex,
                    Count = request.Count,
                    Filter = filter,
                    OrderBy = request.OrderBy,
                    Descending = request.Descending,
                    AllUsers = request.ShowAllUserData
                };
            if (Count != 0)
            {
                req.Count = Count;
            }
            var ret = await Http.PostAsJson<ListUserActionResponse>("api/UserAction/List", req, request.CancellationToken);
            return new ItemsProviderResult<GXUserAction>(ret.Actions, ret.Count);
        }
        catch (TaskCanceledException)
        {
            //Let the table component handle this.
            throw;
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
        return default;
    }

    public void Dispose()
    {
        Notifier.RemoveListener(this);
    }

    /// <summary>
    /// Show user action.
    /// </summary>
    public void OnShow()
    {
        try
        {
            Notifier.ClearStatus();
            if (table?.Active == null)
            {
                throw new Exception(Gurux.DLMS.AMI.Client.Properties.Resources.NoItemIsSelected);
            }
            ClientHelpers.NavigateTo(NavigationManager, Notifier, "/UserAction/Edit/" + table.Active.Id);

        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }


    /// <summary>
    /// Clear the user actions.
    /// </summary>
    public void OnClear()
    {
        ClearConfirmation?.Show();
    }

    /// <summary>
    /// Clear the system error list.
    /// </summary>
    public async Task OnClearConfirmation(ConfirmArgs args)
    {
        try
        {
            if (args.Confirm)
            {
                await Http.PostAsJson<ClearUserActionResponse>("api/UserAction/Clear", new ClearUserAction());
            }
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }
}
